package is.server.model.xtool;

import is.common.exception.RowNotFoundException;
import is.common.interfaces.SectionAdapter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Resolving dependencies
 *
 * @author aspr0
 *         Date: 18.04.12
 *         Time: 22:38
 */
public class SectionArbiter
{
    private Document dbDoc;
    private EmployeeSectionAdapter employeeSectionAdapter;
    private DepartmentSectionAdapter departmentSectionAdapter;

    public SectionArbiter(Document dbDoc)
    {
        this.dbDoc = dbDoc;
    }

    public SectionAdapter selectAdapter(int sectionNum)
    {
        switch (sectionNum)
        {
            case 0:
                return employeeSectionAdapter;

            case 1:
                return departmentSectionAdapter;
        }
        return null;
    }

    public void parseDoc()
    {
        NodeList sections = this.dbDoc.getChildNodes().item(0).getChildNodes();
        Node employeeSection = sections.item(0);
        Node departmentSection = sections.item(1);
        NodeList employeeSectionRows = employeeSection.getChildNodes();
        NodeList departmentSectionRows = departmentSection.getChildNodes();


        employeeSectionAdapter = new EmployeeSectionAdapter(employeeSection, employeeSectionRows, this);
        departmentSectionAdapter = new DepartmentSectionAdapter(departmentSection, departmentSectionRows, this);
    }


    public void initDoc()
    {
        Node root = createDomElement(Name.Tag.root);
        dbDoc.appendChild(root);

        Node employeeSection = createDomElement(Name.Tag.Employee.section);
        Node departmentSection = createDomElement(Name.Tag.Department.section);
        root.appendChild(employeeSection);
        root.appendChild(departmentSection);
    }

    /**
     * @param dbDocAlien document from another server for concatenation
     * @return removed duplicates count
     */
    public int concat(Document dbDocAlien)
    {
        ConcatDupless concatDupless = new ConcatDupless(dbDoc, dbDocAlien);

        int removedDuplicatesCount = concatDupless.process();
        employeeSectionAdapter.updateRowCount();
        departmentSectionAdapter.updateRowCount();

        return removedDuplicatesCount;
    }

    public String[] getEmployeeName(String id) throws RowNotFoundException
    {
        return employeeSectionAdapter.getName(id);
    }

    public void employeeInDepartment(String id, String departmentId) throws RowNotFoundException
    {
        employeeSectionAdapter.inDepartment(id, departmentId);
    }

    public boolean isDepartmentEmpty(String departmentId)
    {
        return employeeSectionAdapter.isDepartmentEmpty(departmentId);
    }

    public void employeeIdExists(String id) throws RowNotFoundException
    {
        employeeSectionAdapter.idExists(id);
    }


    public String getDepartmentId(String name) throws RowNotFoundException
    {
        return departmentSectionAdapter.getId(name);
    }

    public String getDepartmentName(String id) throws RowNotFoundException
    {
        return departmentSectionAdapter.getDepartmentName(id);
    }


    public Element createDomElement(String tagName)
    {
        return dbDoc.createElement(tagName);
    }
}
